package com.sgy.handwrite_ioc_aop.servlet;

import com.sgy.handwrite_ioc_aop.factory.BeanFactory;
import com.sgy.handwrite_ioc_aop.factory.ProxyFactory;
import com.sgy.handwrite_ioc_aop.pojo.Result;
import com.sgy.handwrite_ioc_aop.service.TransferService;
import com.sgy.handwrite_ioc_aop.service.impl.TransferServiceImpl;
import com.sgy.handwrite_ioc_aop.utils.JsonUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Administrator
 * @date 2025/7/13 11:59
 * @description
 */
@RestController
public class TransferServlet {


    // 1. 实例化service层对象
//    private TransferService transferService = new TransferServiceImpl();
//    private TransferService transferService = (TransferService) BeanFactory.getBean("transferService");

    // 从工厂直接获取代理对象，增强事务控制功能
//    private TransferService transferService = (TransferService) ProxyFactory.getInstance().getJdkProxy(BeanFactory.getBean("transferService"));
    // 从beanfactory获取到proxyfactory代理工厂的实例化对象
    private ProxyFactory proxyFactory = (ProxyFactory) BeanFactory.getBean("proxyFactory");
    private TransferService transferService = (TransferService) proxyFactory.getJdkProxy(BeanFactory.getBean("transferService"));

    @RequestMapping("/transferServlet")
    public Result doPost(@RequestParam("fromCardNo") String fromCardNo, @RequestParam("toCardNo") String toCardNo, @RequestParam("money") Integer money){
        Result result = new Result();

        try {
            transferService.transfer(fromCardNo,toCardNo,money);
            result.setStatus("200");
        } catch (Exception e) {
            e.printStackTrace();
            result.setStatus("201");
            result.setMessage(e.toString());
        }
        return result;
    }
}
